/*
 *
 * Copyright (c) 2023 - future
 * @author fzh
 * @email e_341877120_mail@163.com
 * @link https://mvnrepository.com/search?q=io.github.xrfzh.cn
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package cn.xrfzh.file.config;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import cn.xrfzh.file.properties.MinioConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Configuration
public class AmazonS3Config {

	@Resource
	private MinioConfigurationProperties minioConfigurationProperties;

	@Bean("amazonS3Client")
	public AmazonS3 amazonS3Client() {
		// 设置连接时的参数
		ClientConfiguration config = new ClientConfiguration();
		// 设置连接方式为HTTP，可选参数为HTTP和HTTPS
		config.setProtocol(Protocol.HTTP);
		// 设置网络访问超时时间
		config.setConnectionTimeout(5000);
		config.setUseExpectContinue(true);
		AWSCredentials credentials = new BasicAWSCredentials(minioConfigurationProperties.getAccessKey(),
				minioConfigurationProperties.getAccessSecret());
		// 设置Endpoint
		AwsClientBuilder.EndpointConfiguration end_point = new AwsClientBuilder.EndpointConfiguration(
				minioConfigurationProperties.getEndpoint(), Regions.US_EAST_1.name());
		AmazonS3 amazonS3 = AmazonS3ClientBuilder.standard().withClientConfiguration(config)
				.withCredentials(new AWSStaticCredentialsProvider(credentials)).withEndpointConfiguration(end_point)
				.withPathStyleAccessEnabled(true).build();
		return amazonS3;
	}

}
